import os
import time


# node2
def TestIFIO():
    output_file = "/tmp/node2_to_node1.tmp"
    input_file = "/tmp/node1_to_node2.tmp"

    # 1.open pipe
    fin = os.open(input_file, os.O_RDONLY)
    fout = os.open(output_file, os.O_WRONLY)

    # 3.write and read data
    send_str = "I'm fine, thx!"
    while True:
        try:
            recv_str = os.read(fin, 1024).decode()[:-1]
        except:
            print("node1 closed, exit")
            break

        print("recv: ", recv_str)
        os.write(fout, str.encode(send_str))
        print("send: ", send_str)
        time.sleep(0.5)

    os.close(fin)
    os.close(fout)


if __name__ == '__main__':
    TestIFIO()
